"""
脚本说明!

# by bingfeng at 2022-12-17
# 此脚本用来生成图形程序.基于python3版本.使用方法: python CreateSoftware.py

"""

from setting import tools, line_count, themes, width, button_width, height
import subprocess

# 生成bingfengsec.py文件
def gen_click():
    no = 1
    with open('bingfengsec.py','w',encoding='utf-8')as f:
        f.write(
"""\"\"\"
脚本说明!

# by bingfeng at 2022-12-17
#此脚本是一个图形化的渗透测试工具集

\"\"\"

import subprocess, sys, argparse

from setting import java8_path, java9_path

parser = argparse.ArgumentParser(prog="BingFengSecGui", usage="冰封工具箱v1.0", description="冰封工具箱常用参数如下:",
                                 epilog="更详细信息见网站:http://www.wangzhuanzhijia.cf")
parser.add_argument("-u", "--url", required="ture", help="需要扫描的网址或者ip地址")
parser.add_argument("-p", "--protocol", default="https", choices=["https", "http"], help="web协议类型: https 或 http")
parser.add_argument("-e", "--network", default="eth0", help="选择扫描使用的网卡: eth0等")

args = parser.parse_args()

if "://" in vars(args)["url"]:
    url_1 = vars(args)["url"].split("://")
    web_protocol = url_1[0]
    url_or_ip = url_1[1]
else:
    web_protocol = vars(args)["protocol"]
    url_or_ip = vars(args)["url"]

url_list = vars(args)["url"].split(".")
len_url_list = len(url_list)
ip = url_or_ip

if len(url_list) == 4:
    for num in url_list:
        if num.isdigit() and 0 <= int(num) <= 255:
            pass
        else:
            dig_ip = subprocess.Popen('dig {0} | grep -A 2 "ANSWER SECTION" | grep {0} | sed "s/\t/ /gp" | grep "IN A" | head -n 1 | awk -F "IN A " "{{print \\$2}}"'.
                                      format(url_or_ip), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            ip = dig_ip.stdout.read().decode().strip()
            break
else:
    dig_ip = subprocess.Popen('dig {0} | grep -A 2 "ANSWER SECTION" | grep {0} | sed "s/\t/ /gp" | grep "IN A" | head -n 1 | awk -F "IN A " "{{print \\$2}}"'.
                              format(url_or_ip), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    ip = dig_ip.stdout.read().decode().strip()

eth = vars(args)["network"]

class BingFengSec():
"""
        )
        for t in tools.values():
            for cmd in t.values():
                f.write("""
    def btn_{no}():
        subprocess.Popen({cmd}, shell=True)
""".format(no=no,cmd=cmd))
                no += 1

# 生成bingfengsecgui.py文件
def gen_body():
    i = 0
    with open('bingfengsecgui.py','w',encoding='utf-8')as f:
        f.write(
"""import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from bingfengsec import BingFengSec

root = ttk.Window(
                title="【自用渗透工具集-linux】 by 冰封/BingFeng",
                themename="{themes}",
                size=({width}, {height}),
                resizable=(True, True),
                )
f = ttk.Frame(root)
f.pack(pady=5, fill=X, side=TOP)
pk = ttk.Notebook(f)
pk.pack(
    side=LEFT,
    padx=(10, 0),
    expand=YES,
    fill=BOTH
)
        """.format(themes=themes,width=width,height=height))
        no = 0
        for title,tool in tools.items():
            f.write("""
#################### {title} ####################
f_{number} = ttk.Frame(pk)
            """.format(title=title,number=i))
            col = 0
            row = 1
            for tool_name,tool_info in tool.items():
                col += 1
                no += 1
                if col % (line_count+1) == 0:
                    col = 1
                    row += 1
                f.write("""
ttk.Button(f_{number},text="{tool}", bootstyle=(PRIMARY, "success-outline-toolbutton"),
            width={button_width}, command=BingFengSec.btn_{no}).grid(row={row},column={col},padx=20,pady=10)
                """.format(number=i,tool=tool_name,button_width=button_width,no=no,row=row,col=col))
            f.write("""
pk.add(f_{number}, text='     {title}    ')
            """.format(number=i,title=title))
            
            i += 1
        f.write("""
root.mainloop()""")    
if __name__ == "__main__":
    gen_click()
    gen_body()
